#include <iostream>
#include <string>
#include <stack>
#include <cctype>
using namespace std;

const int N = 1000010;

struct Node {
    int op;
    int l, r;
    int val;
    int vid;
} t[N];

string in;
int n, v[100010], result;
int cnt;
bool imp[100010];

int build() {
    stack<int> st;
    int i = 0;
    
    while (i < in.length()) {
        if (in[i] == 'x') {
            int num = 0;
            i++;
            while (i < in.length() && isdigit(in[i])) {
                num = num * 10 + (in[i] - '0');
                i++;
            }
            int id = cnt++;
            t[id] = {0, -1, -1, v[num], num};
            st.push(id);
        }
        else if (in[i] == '&') {
            int r = st.top(); st.pop();
            int l = st.top(); st.pop();
            t[cnt] = {1, l, r, t[l].val & t[r].val, -1};
            st.push(cnt++);
            i += 2;
        }
        else if (in[i] == '|') {
            int r = st.top(); st.pop();
            int l = st.top(); st.pop();
            t[cnt] = {2, l, r, t[l].val | t[r].val, -1};
            st.push(cnt++);
            i += 2;
        }
        else if (in[i] == '!') {
            int l = st.top(); st.pop();
            t[cnt] = {3, l, -1, !t[l].val, -1};
            st.push(cnt++);
            i += 2;
        }
        else {
            i++;
        }
    }
    return st.top();
}

void mark(int u) {
    if (t[u].op == 0) {
        imp[t[u].vid] = true;
        return;
    }
    
    int l = t[u].l, r = t[u].r;
    
    if (t[u].op == 1) {
        if (t[l].val == 1) mark(r);
        if (t[r].val == 1) mark(l);
    }
    else if (t[u].op == 2) {
        if (t[l].val == 0) mark(r);
        if (t[r].val == 0) mark(l);
    }
    else if (t[u].op == 3) {
        mark(l);
    }
}

int main() { 
    getline(cin, in);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i];
    }
    
    int root = build();
    result = t[root].val;
    mark(root);
    
    int q, x;
    cin >> q;
    while (q--) {
        cin >> x;
        if (imp[x]) {
            cout << !result << endl;
        } else {
            cout << result << endl;
        }
    }
    
    return 0;
}

